<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>购物车</title>
        <%@ include file="/page/menu/head.jsp" %>
    </head>
    <body>
        <div id="header">
            <%@include file="/page/menu/login_menu.jsp"%>
            <script>
                function deleteItem(dom){
                    var obj1=dom.parentNode.children[0];
                    return window.confirm("确定要删除商品"+obj1.innerText+"吗？");
                    return false;
                }
                function UpdateCount(dom){

                    var judge=confirm("确定要修改数量吗?");
                    if(judge==false)
                        dom.value=dom.defaultValue;
                    else{
                        window.location.href="http://localhost:8080/Javaweb/CartServlet?action=updateCount&count="+dom.value+"&id="+dom.name;
                    }
                }
                function clearCart(){
                    return window.confirm("确定要清空购物车吗？");
                    return false;
                }
                function ispay(){
                    return window.confirm("确定要结账吗？");
                    return false;
                }
            </script>
        </div>
        <div id="main">

            <table>
                <tr>
                    <td>商品名称</td>
                    <td>数量</td>
                    <td>单价</td>
                    <td>金额</td>
                    <td>操作</td>
                </tr>
                <c:if test="${empty sessionScope.cart.items}">
                    <%--如果购物车空的情况--%>
                    <tr>
                        <td colspan="5"><a href="index.jsp">亲，当前购物车为空，请先去购物</a> </td>
                    </tr>
                </c:if>
                <c:if test="${not empty sessionScope.cart.items}">
                    <%--如果购物车非空的情况--%>
                    <c:forEach items="${sessionScope.cart.items}" var="item">
                        <tr>
                            <td>${item.value.name}</td>
                            <td>
                            <input type="text" name="${item.value.id}"
                                   onchange="return UpdateCount(this)"
                                   value="${item.value.count}">
                            </td>
                            <td>${item.value.price}</td>
                            <td>${item.value.totalPrice}</td>
                            <td><a onclick="return deleteItem(this)" href="CartServlet?action=deleteItem&id=${item.value.id}">删除</a></td>
                        </tr>
                    </c:forEach>
                </c:if>
            </table>
            <%--如果购物车非空才输出页面的内容--%>
            <c:if test="${not empty sessionScope.cart.items}">
                <div class="cart_info">
                    <span class="cart_span">购物车中共有<span class="b_count">${sessionScope.cart.totalCount}</span>件商品</span>
                    <span class="cart_span">总金额<span class="b_price">${sessionScope.cart.totalPrice}</span>元</span>
                    <span class="cart_span"><a onclick="return clearCart()" href="CartServlet?action=clear">清空购物车</a></span>
                    <span class="cart_span"><a  onclick="return ispay()" href="OrderServlet?action=pay">结账</a></span>
                </div>
            </c:if>

        </div>
    </body>
</html>
